package com.example.qqbotdemo.config;

import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.auth.BotAuthorization;
import net.mamoe.mirai.utils.BotConfiguration;
import net.mamoe.mirai.utils.LoginSolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.cssxsh.mirai.tool.FixProtocolVersion;

import java.io.File;

@Configuration
@Log4j2
public class BotConfig {

    @Resource
    private MiraiLogConfig miraiLogConfig;

    @Bean("qqBot")
    public Bot newBot() {
//        FixProtocolVersion.update();
//        log.info("FixProtocolVersion: " + FixProtocolVersion.info());
//        FixProtocolVersion.fetch(BotConfiguration.MiraiProtocol.ANDROID_PAD, "latest");
        FixProtocolVersion.load(BotConfiguration.MiraiProtocol.ANDROID_PAD);

        //qq号
        Long qqNumber = 0000L;
        //密码
        String password = "xxxxxx";

        Bot bot = BotFactory.INSTANCE.newBot(qqNumber, BotAuthorization.byPassword(password), configuration ->{
            configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);

            configuration.setLoginSolver(LoginSolver.Default);
            configuration.fileBasedDeviceInfo();
            configuration.setHeartbeatStrategy(BotConfiguration.HeartbeatStrategy.STAT_HB);
            configuration.setWorkingDir(new File("./"));
//            configuration.setNetworkLoggerSupplier(bot1 -> miraiLogConfig);
            configuration.noNetworkLog();
            //日志重定向
            configuration.setBotLoggerSupplier(bot1 -> miraiLogConfig);
        });
        return bot;
    }

}
